package com.shopping.mall.core.module.favorite.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.favorite.entity.Favorite;
import com.shopping.mall.core.module.favorite.service.FavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "收藏")
@RestController
@RequestMapping("/favorite")
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;

    @ApiOperation("收藏夹添加商品")
    @GetMapping("/add")
    public Result<Object> add(@RequestParam String goodsId) {
        return favoriteService.add(goodsId);
    }

    @ApiOperation("查看收藏夹(分页)")
    @GetMapping("/favoritePage")
    public Result<Object> favoritePage(@RequestParam(name = "current", defaultValue = "1") Integer current,
                                       @RequestParam(name = "size", defaultValue = "10") Integer size) {
        return favoriteService.favoritePage(current, size);
    }

    @ApiOperation("查看收藏夹(不分页)")
    @GetMapping("/favorite")
    public Result<Object> favorite() {
        return favoriteService.favorite();
    }

    @ApiOperation("取消收藏")
    @DeleteMapping("/delete")
    public Result<Object> delete(String goodsId) {
        return favoriteService.delete(goodsId);
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<Favorite>> getFavoritePage(
            @RequestParam(name = "current", defaultValue = "1") Integer current,
            @RequestParam(name = "size", defaultValue = "10") Integer size,
            Favorite favorite) {
        IPage<Favorite> favoritePage = favoriteService.getFavoritePage(current, size, favorite);
        return Result.OK(favoritePage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<Favorite> update(@Validated(Favorite.update.class) @RequestBody Favorite favorite) {
        favoriteService.updateById(favorite);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(Favorite.insert.class) @RequestBody Favorite favorite) {
        favoriteService.save(favorite);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Favorite> getById(@RequestParam String id) {
        Favorite favorite = favoriteService.getById(id);
        return Result.OK(favorite);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<Favorite>> getAll(Favorite favorite) {
        List<Favorite> favoriteList = favoriteService.getAll(favorite);
        return Result.OK(favoriteList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id) {
        return favoriteService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids) {
        return favoriteService.batchDelete(ids);
    }
}
